package com.wanmait.mavendemo.dao.impl;



import com.wanmait.mavendemo.dao.QuestionTypeDAO;
import com.wanmait.mavendemo.pojo.QuestionType;
import com.wanmait.mavendemo.util.DBHelper;
import com.wanmait.mavendemo.util.Pager;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository("questionTypeDAO")
public class QuestionTypeDAOImpl implements QuestionTypeDAO {

    public void updateSort(Integer id,Integer sort)
    {

    }

    @Override
    public void increaseQuestionCount(Integer id) {

    }

    @Override
    public void decreaseQuestionCount(Integer id) {

    }

    @Override
    public List<QuestionType> findAll() {
        List<QuestionType> questionTypes = new ArrayList<>();
        String sql = "select id,title,question_count,sort,enable from question_type where enable=1 order by sort desc";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBHelper.getDBhelper().getConn();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            QuestionType questionType = null;
            while (resultSet.next())
            {
                questionType = new QuestionType();
                questionType.setId(resultSet.getInt("id"));
                questionType.setTitle(resultSet.getString("title"));
                questionType.setQuestionCount(resultSet.getInt("question_count"));
                questionType.setSort(resultSet.getInt("sort"));
                questionType.setEnable(true);
                questionTypes.add(questionType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.getDBhelper().close(resultSet,preparedStatement,connection);
        }

        return questionTypes;
    }

    @Override
    public QuestionType findById(Integer id) {
        QuestionType questionType = null;
        String sql = "select id,title,question_count,sort,enable from question_type where enable=1 and id=?";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBHelper.getDBhelper().getConn();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next())
            {
                questionType = new QuestionType();
                questionType.setId(resultSet.getInt("id"));
                questionType.setTitle(resultSet.getString("title"));
                questionType.setQuestionCount(resultSet.getInt("question_count"));
                questionType.setSort(resultSet.getInt("sort"));
                questionType.setEnable(true);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.getDBhelper().close(resultSet,preparedStatement,connection);
        }
        return questionType;
    }

    @Override
    public List<QuestionType> findAll(Pager pager) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public void insert(QuestionType object) {
        String sql = "insert into question_type(title,sort) values(?,?)";
        DBHelper.getDBhelper().update(sql,object.getTitle(),object.getSort());
    }

    @Override
    public void update(QuestionType object) {
        String sql = "update question_type set title=?,sort=? where id=?";
        DBHelper.getDBhelper().update(sql,object.getTitle(),object.getSort(),object.getId());
    }

    @Override
    public void delete(Integer id) {
        String sql = "update question_type set enable=0 where id=?";
        DBHelper.getDBhelper().update(sql,id);
    }

    @Override
    public void delete(String ids) {

    }
}
